// utils/location.js
const app = getApp();

/**
 * 获取用户当前位置
 */
function getCurrentLocation() {
  return new Promise((resolve, reject) => {
    wx.getLocation({
      type: 'gcj02', // 使用国测局坐标
      success: (res) => {
        resolve({
          latitude: res.latitude,
          longitude: res.longitude
        });
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 更新技师位置
 */
function updateTechnicianLocation(latitude, longitude, address = '') {
  return new Promise((resolve, reject) => {
    app.request({
      url: '/accounts/location/update/',
      method: 'POST',
      data: {
        latitude: latitude,
        longitude: longitude,
        address: address
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 获取技师当前位置
 */
function getTechnicianLocation() {
  return new Promise((resolve, reject) => {
    app.request({
      url: '/accounts/location/my/',
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 获取附近技师
 */
function getNearbyTechnicians(latitude, longitude, maxDistance = 10) {
  return new Promise((resolve, reject) => {
    app.request({
      url: '/accounts/nearby/',
      data: {
        latitude: latitude,
        longitude: longitude,
        max_distance: maxDistance
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 添加服务区域
 */
function addServiceArea(name, latitude, longitude, radius = 5) {
  return new Promise((resolve, reject) => {
    app.request({
      url: '/accounts/service-area/add/',
      method: 'POST',
      data: {
        name: name,
        latitude: latitude,
        longitude: longitude,
        radius: radius
      },
      success: (res) => {
        if (res.statusCode === 201) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 获取服务区域
 */
function getServiceAreas() {
  return new Promise((resolve, reject) => {
    app.request({
      url: '/accounts/service-area/list/',
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 删除服务区域
 */
function deleteServiceArea(areaId) {
  return new Promise((resolve, reject) => {
    app.request({
      url: `/accounts/service-area/${areaId}/delete/`,
      method: 'DELETE',
      success: (res) => {
        if (res.statusCode === 204) {
          resolve();
        } else {
          reject(res.data);
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

module.exports = {
  getCurrentLocation,
  updateTechnicianLocation,
  getTechnicianLocation,
  getNearbyTechnicians,
  addServiceArea,
  getServiceAreas,
  deleteServiceArea
};